/*
* Copyright (c) 2016 . All Rights Reserved.
*/
package com.cgmnx.admin.web.converter.entity;

import com.cgmnx.admin.common.bean.CurrentUserInfo;
import com.cgmnx.admin.common.bean.UserContext;
import com.cgmnx.admin.common.constants.WebConstant;
import com.cgmnx.admin.dao.entity.rbac.UserEntity;
import com.cgmnx.admin.model.enums.rbac.UserStatus;
import com.cgmnx.admin.web.converter.Convert;
import com.cgmnx.admin.web.model.param.UserParam;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author: created  Date: 16/12/13 Time: 下午6:32
 * @version: 1.0
 */
@Component
public class UserEntityConverter implements Convert<UserParam, UserEntity> {

    private static final int SUFFIX_LENGTH = 4;

    @Autowired
    //private MySQLMaxValueIncrementer userIdGenerator;

    private String generateUserId() {
//        long nextKey = userIdGenerator.nextLongValue();
//        IdentityGenerator generator = new DefaultIdentityGenerator(SystemCodeType.User, SUFFIX_LENGTH);
//
//        return userIdGenerator.getGroupValue() + generator.generateOrderId() + nextKey;
        return "";
    }


    @Override
    public UserEntity convert(UserParam input) {

        if (input == null) {
            return null;
        }

        UserEntity entity = new UserEntity();
        entity.setUserId(generateUserId());
        entity.setRoleList(Lists.newArrayList(input.getRoleId()));
        entity.setUserName(input.getUserName());
        entity.setPassword(input.getPassword());
        entity.setUserPhone(input.getUserPhone());
        entity.setUserEmail(input.getUserEmail());

        entity.setStatus(UserStatus.ENABLE);

        Integer id = input.getId();
        if (id != null) {
            entity.setId(input.getId());
        }
        CurrentUserInfo userInfo = UserContext.getUserInfo();
        if (userInfo != null) {
            entity.setCreateUser(userInfo.getUserId());
            entity.setUpdateUser(userInfo.getUserId());
        } else {
            entity.setCreateUser(WebConstant.CURRENT_LOGIN_USER);
            entity.setUpdateUser(WebConstant.CURRENT_LOGIN_USER);
        }

        entity.setDescription(input.getDesc());
        return entity;
    }

    @Override
    public List<UserEntity> convert(List<UserParam> t) {
        return null;
    }
}